package model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.json.JSONArray;
import org.json.JSONObject;

@Entity
public class Item implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Column
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer idItem;
	
//	@JoinColumn(name="idCategory", referencedColumnName="idCategory")
//	@ManyToOne
//	private Category category;
	
    @JoinTable(name = "item_category", joinColumns =  {
        @JoinColumn(name = "idItem", referencedColumnName = "idItem", nullable=false)
    }, inverseJoinColumns =  {
        @JoinColumn(name = "idCategory", referencedColumnName = "idCategory", nullable=false)
    })
    @ManyToMany(fetch=FetchType.EAGER)    
    private Set<Category> categories;
	
	
	@JoinColumn(name="idMember", referencedColumnName="idMember")
	@ManyToOne
	private Member member;	
	
	@Column
	private String name;
	
	@Column
	private String description;
	
	@Column
	private Double latitude;
	
	@Column
	private Double longitude;
	
	@Column
	private String street;
	
	@Column
	private String city;

	@Column
	private String state;
	
	@Column
	private String country;
	
	@Column
	private String zipcode;
	
	@Column
	private String phone;

	@Column
	private String cellphone;
	
	@Column
	private String email;
	
	@Column
	private String skype;
	
	@Column
	private String url;
	
	@OneToMany(mappedBy="item")
	private Set<Photo> photos;
	
	public Item() {
	}
	
	public Item(Integer idItem) {
		this.idItem = idItem;
	}
	
	@Override
	public String toString() {
		try{
			JSONObject jsonObj = new JSONObject();
			jsonObj.put("idItem", this.getIdItem());
			jsonObj.put("name", this.getName());
			jsonObj.put("description", this.getDescription());
			
//			if(this.getCategory() != null){
//				jsonObj.put("category", new JSONObject(this.getCategory().toString()));
//			}

			if(this.getCategories() != null){
				JSONArray jsonArray = new JSONArray();
				
				for (Category category : this.getCategories()) {
					jsonArray.put(new JSONObject(category.toString()));
				}
				
				jsonObj.put("categories", jsonArray);				
			}
			
			JSONObject location = new JSONObject();
			location.put("latitude", this.getLatitude());
			location.put("longitude", this.getLongitude());
			location.put("street", this.getStreet());
			location.put("state", this.getState());
			location.put("city", this.getCity());
			location.put("country", this.getCountry());
			location.put("zipcode", this.getZipcode());
			jsonObj.put("location", location);
			
			JSONObject contact = new JSONObject();
			contact.put("phone", this.getPhone());
			contact.put("cellphone", this.getCellphone());
			contact.put("email", this.getEmail());
			contact.put("skype", this.getSkype());
			contact.put("url", this.getUrl());
			jsonObj.put("contact", contact);
			
			return jsonObj.toString();
		}catch (Exception e){
			throw new RuntimeException(e);
		}
	}	

	public Integer getIdItem() {
		return idItem;
	}

	public void setIdItem(Integer idItem) {
		this.idItem = idItem;
	}

//	public Category getCategory() {
//		return category;
//	}
//
//	public void setCategory(Category category) {
//		this.category = category;
//	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Double getLatitude() {
		return latitude;
	}

	public void setLatitude(Double latitude) {
		this.latitude = latitude;
	}

	public Double getLongitude() {
		return longitude;
	}

	public void setLongitude(Double longitude) {
		this.longitude = longitude;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getZipcode() {
		return zipcode;
	}

	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getCellphone() {
		return cellphone;
	}

	public void setCellphone(String cellphone) {
		this.cellphone = cellphone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getSkype() {
		return skype;
	}

	public void setSkype(String skype) {
		this.skype = skype;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Set<Photo> getPhotos() {
		return photos;
	}

	public void setPhotos(Set<Photo> photos) {
		this.photos = photos;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public Member getMember() {
		return member;
	}

	public void setCategories(Set<Category> categories) {
		this.categories = categories;
	}

	public Set<Category> getCategories() {
		return categories;
	}

	
}
